import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
import systemInformation from '@/views/system/information'
import messageCreate from '@/views/message/create'
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/plantlib/list'
  }
]

// 动态加载
export const asyncRoutes = [
  // 植物库
  {
    path: '/plantlib',
    component: Layout,
    name: 'plantlib',
    meta: { title: '植物库', icon: 'plant' },
    redirect: '/plantlib/list',
    children: [{
      path: 'list',
      name: 'plantIndex',
      component: () => import('@/views/plantLib/plantData/list'),
      meta: { title: '植物列表', activeMenu: '/plantlib/list' }
    }, {
      path: 'add',
      name: 'plantAdd',
      component: () => import('@/views/plantLib/plantData/add'),
      meta: { title: '新增植物', activeMenu: '/plantlib/list' },
      hidden: true
    }, {
      path: 'detail/:id',
      name: 'plantDetail',
      component: () => import('@/views/plantLib/plantData/detail'),
      meta: { title: '植物详情', activeMenu: '/plantlib/list' },
      hidden: true
    }, {
      path: 'edit/:id',
      name: 'plantEdit',
      component: () => import('@/views/plantLib/plantData/edit'),
      meta: { title: '植物编辑', activeMenu: '/plantlib/list' },
      hidden: true
    }, {
      path: 'identify',
      name: 'identify',
      component: () => import('@/views/plantLib/identify/list'),
      meta: { title: '植物识别基础', activeMenu: '/plantlib/identify' }
    }, {
      path: 'identify/detail/:id',
      name: 'identifyDetail',
      component: () => import('@/views/plantLib/identify/detail'),
      meta: { title: '植物识别详情', activeMenu: '/plantlib/identify' },
      hidden: true
    }, {
      path: 'skill',
      name: 'skill',
      component: () => import('@/views/plantLib/skill/menu'),
      meta: { title: '辅助野外生存的植物知识与技巧', activeMenu: '/plantlib/skill' }
    }, {
      path: 'skill/list/:id',
      name: 'skillList',
      component: () => import('@/views/plantLib/skill/list'),
      meta: { title: '辅助野外生存的植物知识与技巧', activeMenu: '/plantlib/skill' },
      hidden: true
    }, {
      path: 'skill/detail/:id',
      name: 'skillDetail',
      component: () => import('@/views/plantLib/skill/detail'),
      meta: { title: '辅助野外生存的植物知识与技巧', activeMenu: '/plantlib/skill' },
      hidden: true
    }, {
      path: 'skill/manage',
      name: 'skillManage',
      component: () => import('@/views/plantLib/skill/manage/list'),
      meta: { title: '辅助野外生存的植物知识与技巧', activeMenu: '/plantlib/skill' },
      hidden: true
    }, {
      path: 'skill/manage/edit/:id',
      name: 'skillEdit',
      component: () => import('@/views/plantLib/skill/manage/edit'),
      meta: { title: '辅助野外生存的植物知识与技巧', activeMenu: '/plantlib/skill' },
      hidden: true
    }]
  },
  // 习题库
  {
    path: '/question',
    component: Layout,
    name: 'question',
    meta: { title: '习题库', icon: 'el-icon-s-help' },
    redirect: '/question/list',
    children: [
      {
        path: 'list',
        name: 'questionList',
        component: () => import('@/views/question/list'),
        meta: { title: '习题列表', activeMenu: '/question/list' }
      },
      {
        path: 'detail/:id',
        name: 'questionDetail',
        component: () => import('@/views/question/detail'),
        meta: { title: '习题详情', activeMenu: '/question/list' },
        hidden: true
      },
      {
        path: 'answer/:id',
        name: 'questionAnswer',
        component: () => import('@/views/question/answer'),
        meta: { title: '习题回答', activeMenu: '/question/list' },
        hidden: true
      }
    ]
  },
  // 留言
  {
    path: '/message',
    component: Layout,
    name: 'message',
    redirect: '/message/list',
    meta: { title: '留言板', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'list',
        name: 'messageList',
        component: () => import('@/views/message/list'),
        meta: { title: '留言列表', activeMenu: '/message/list' }
      },
      {
        path: 'detail/:id',
        name: 'messageDetail',
        component: () => import('@/views/message/detail'),
        meta: { title: '留言详情', activeMenu: '/message/list' },
        hidden: true
      },
      {
        path: 'create',
        name: 'messageCreate',
        component: messageCreate,
        meta: { title: '新增留言', activeMenu: '/message/list' },
        hidden: true
      },
      {
        path: 'reply/:id',
        name: 'messageReply',
        component: () => import('@/views/message/reply'),
        meta: { title: '留言回复', activeMenu: '/message/list' },
        hidden: true
      }
    ]
  },
  // 系统管理
  {
    path: '/system',
    component: Layout,
    name: 'system',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    redirect: '/system/account',
    children: [
      {
        path: 'account',
        name: 'systemAccount',
        component: () => import('@/views/system/account'),
        meta: { title: '账号管理' }
      },
      {
        path: 'role',
        name: 'systemRole',
        component: () => import('@/views/system/role'),
        meta: { title: '角色管理' }
      },
      {
        path: 'information',
        name: 'systemInformation',
        component: systemInformation,
        meta: { title: '展示信息' }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
   router.replace(targetPath);
  }
 });
export default router
